<template>
	<xn-form-container
		title="分配客户"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '0px' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" class="p-10">
			<a-form-item label="选择分配人" name="personId">
				<a-select
					v-model:value="formData.personId"
					:options="allocateOptions"
					:field-names="{
						label: 'name',
						value: 'id'
					}"
					placeholder="请选择分配人"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<div class="flex items-center justify-between">
				<div>已选{{ list.length }}个客户</div>
				<a-space>
					<a-button type="primary" :loading="submitLoading" @click="onSubmit">确定</a-button>
					<a-button style="margin-right: 8px" @click="onClose">取消</a-button>
				</a-space>
			</div>
		</template>
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import highSeasApi from '@/api/biz/highSeasApi'
	import { Modal } from 'ant-design-vue'
	import { createVNode } from 'vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'

	const submitLoading = ref(false)

	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const props = defineProps({
		highSeasId: {
			type: String,
			required: true
		}
	})
	const emit = defineEmits({ successful: null })

	// 表单数据
	const formData = ref({})

	const list = ref([])
	const allocateOptions = ref([])

	// 打开抽屉
	const onOpen = (value) => {
		visible.value = true
		list.value = value
		highSeasApi.allocatorList(props).then((data) => {
			allocateOptions.value = data
		})
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		personId: [required('请选择分配人')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			highSeasApi
				.allocate({
					...props,
					...formData.value,
					poolIdParamList: list.value
				})
				.then(() => {
					onClose()
					formRef.value.resetFields()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}

	const receiveAction = (params, successFn) => {
		Modal.confirm({
			title: '提示',
			icon: createVNode(ExclamationCircleOutlined),
			content: '确认领取吗？',
			okText: '确定',
			okType: 'warning',
			cancelText: '取消',
			onOk() {
				console.log('OK')
				highSeasApi.receive(params).then(() => {
					successFn && successFn()
				})
			},
			onCancel() {
				console.log('Cancel')
			}
		})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen,
		receiveAction
	})
</script>

<style scoped type="less"></style>
